package emt.friendtracking;

import java.util.ArrayList;
import java.util.HashMap;
import emt.friendtracking.contact.EmtContactsActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends BaseActivity
{
	private static final String TAG = MainActivity.class.getSimpleName();
	
	private Button smsButton;
	private ListView history;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		Log.i(TAG, "onCreated."); //TODO "onCreated."
		
        setContentView(R.layout.main);
        
        smsButton = (Button) findViewById(R.id.smsButton_main);
        history = (ListView) findViewById(R.id.history_ListView_main);
        
        smsButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v)
			{
				Log.i(TAG, "Request accepted. EmtContactsActivity is called.");
				getApplicationContext().startActivity(new Intent(getApplicationContext(), EmtContactsActivity.class).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
				finish();
			}
        });
        
        ArrayList<HashMap<String,String>> elements =  new ArrayList<HashMap<String,String>>();
        
        SimpleAdapter adapter = new SimpleAdapter(MyApplication.getAppContext(),
        			elements,
        			R.layout.main_row, 
        			new String[] { "arrow", "name","date","location_me", "location_other" },
        			new int[] { R.id.mainScreenImageViewDirection, R.id.mainScreenTextViewName, R.id.mainScreenTextViewDate, R.id.mainScreenTextCoordinatesMe, R.id.mainScreenTextCoordinatesOther }
        		);
        
        Data d = new Data();
        
        Cursor historyCursor = d.getHistory();
        
        historyCursor.moveToFirst();
        while (historyCursor.isAfterLast() == false)
        {
        	HashMap<String,String> rowInformation = new HashMap<String,String>();
        	
        	String direction = historyCursor.getString(historyCursor.getColumnIndex(Data.C_DIRECTION));

        	rowInformation.put("name", historyCursor.getString(historyCursor.getColumnIndex(Data.C_SOURCE_NAME)));
        	String date = historyCursor.getString(historyCursor.getColumnIndex(Data.C_DATE));
        	String year = date.substring(0, 4);
        	String month = date.substring(4, 6);
        	String day = date.substring(6, 8);
        	String hour = date.substring(8, 10);
        	String minute = date.substring(10, 12);
        	rowInformation.put("date", year + "-" + month + "-" + day + " " + hour + ":" + minute);
        	
        	double longi_me = historyCursor.getDouble(historyCursor.getColumnIndex(Data.C_LONGITUDE_ME));
        	double lati_me = historyCursor.getDouble(historyCursor.getColumnIndex(Data.C_LATITUDE_ME));
        	rowInformation.put("location_me", Double.toString(lati_me) + " : " + Double.toString(longi_me));
        	
        	if (direction.equalsIgnoreCase(Data.DIRECTION.INCOMING.name()))
        	{
        		rowInformation.put("arrow", Integer.toString(R.drawable.arrow_received));
            	double longi_other = historyCursor.getDouble(historyCursor.getColumnIndex(Data.C_LONGITUDE_OTHER));
            	double lati_other = historyCursor.getDouble(historyCursor.getColumnIndex(Data.C_LATITUDE_OTHER));
        		rowInformation.put("location_other", Double.toString(lati_other) + " : " + Double.toString(longi_other));
        	}
        	else
        	{	
        		rowInformation.put("arrow", Integer.toString(R.drawable.arrow_sent));
            	rowInformation.put("location_other", "");
        	}
        	elements.add(rowInformation);
        	
            historyCursor.moveToNext();
        }
        
        d.close();
        history.setAdapter(adapter);
	}
	
	@Override
	protected boolean isRouteDisplayed()
	{
		return false;
	}

}
